<?php
class CMRPageTest extends CMRPage
{
	
	private function ImportComposer()
	{
		$html="";
		$sql = "SELECT * FROM ZImportComposer";
		$this->Database->ExecuteSQL($sql);
		
		$database = $this->Database->Recopy();
		$date = new DateTime();
		
		while($row=$this->Database->getRow())
		{
			$html .= "<p>".$row['ComposerID']." - ".$row['Surname'].", ".$row['FirstName']." ".$row['SecondNames']."</p>\n";

			// need to create in Composers: 
			// ComposerID
			// Surname
			// FirstName
			// SecondNames
			// DOB
			// DOD
			// Biography
			
			// and in Resources
			// generate ResourceID
			// ResourceTitle
			// ResourceDescription
			// ResourceKeywords
			// ResourceTypeID
			// ... categories
			// Status
			// CreatedBy
			// CreatedWhen
/*			
		$date = new DateTime();
		$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, ActionUserID, ActionComment) ";
		$sql .= "VALUES ('".$ParentTableID."',$actionType,'".$date->format('Y-m-d H:i:s')."','".$_POST['UserID']."','".$_POST['ActionComment']."')";
*/			
			
			$resourceTitle = $row['Surname'].", ".$row['FirstName']." ".$row['SecondNames'];
			$resourceDescription = "Composer information on ".$row['FirstName']." ".$row['SecondNames']." ".$row['Surname'];
			$resourceKeywords = $row['Surname']." ".$row['FirstName']." ".$row['SecondNames']; 
			
			$sql = "INSERT INTO Resources (ResourceTitle,ResourceDescription,ResourceKeywords,ResourceTypeID,StatusTypeID,CreatedBy,CreatedWhen)";
			$sql .= "VALUES ('".$resourceTitle."','".$resourceDescription."','".$resourceKeywords."','3','2','".$_POST['UserID']."','".$date->format('Y-m-d H:i:s')."')";
			echo "\n".$sql."<br>\n";
			$database->ExecuteSQL($sql);
//			echo $sql.mysql_error()."<br>";
//			echo mysql_insert_id()."<br>";
			$ResourceID = mysql_insert_id();
			
			
			$sql = "INSERT INTO Composers (ComposerID,ResourceIDP,Surname,FirstName,SecondNames,DOB,DOD,Biography) \n";
			$sql .= "VALUES ('".$row['ComposerID']."','".$ResourceID."','".$row['Surname']."','".$row['FirstName']."','".$row['SecondNames']."','0001/01/01','0001/01/01','')";
			echo "\n".$sql."<br>\n";
			$database->ExecuteSQL($sql);
			
			$sql = "INSERT INTO ChoralCategorys (ResourceID, ChoralCategoryNameID) VALUES ('".$ResourceID."','1')";
			echo "\n".$sql."<br>\n";
			$database->ExecuteSQL($sql);

			$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, ActionUserID, ActionComment) ";
			$sql .= "VALUES ('".$ResourceID."','2','".$date->format('Y-m-d H:i:s')."','".$_POST['UserID']."','Automated import')";
			echo $sql."<br>";
			$database->ExecuteSQL($sql);
			
			//		
			
		}

		return $html;
		
	}

	private function ImportPieces()
	{
		$html="";
		
		$sql = "SELECT * FROM Composers";
		$this->Database->ExecuteSQL($sql);
		$composers = array();
		while($row=$this->Database->getRow())
		{
			$Arr2ndNames = explode(" ",$row['SecondNames']);
			$secondInitials = substr($row['FirstName'],0,1);
			foreach($Arr2ndNames as $name)$secondInitials .= substr($name,0,1);
			$composers[$row['ComposerID']]=$row['Surname'].", ".$secondInitials;
		}

		$sql = "SELECT * FROM ZImportPieces";
		$this->Database->ExecuteSQL($sql);
		
		$database = $this->Database->Recopy();
		$date = new DateTime();
		
		while($row=$this->Database->getRow())
		{
			// need to create in RepertoireItems: 
			// ComposerIDT
			// Title
			// Arranger
			// Key
			// Organ
			// Voices
			// Publisher
			// Copyright
			// Text
			
			// and in Resources
			// generate ResourceID
			// ResourceTitle
			// ResourceDescription
			// ResourceKeywords
			// ResourceTypeID
			// ... categories
			// Status
			// CreatedBy
			// CreatedWhen
/*			
		$date = new DateTime();
		$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, ActionUserID, ActionComment) ";
		$sql .= "VALUES ('".$ParentTableID."',$actionType,'".$date->format('Y-m-d H:i:s')."','".$_POST['UserID']."','".$_POST['ActionComment']."')";
*/			
			
			$resourceTitle = $composers[$row['Composer']]." ".$row['Title'];
			$resourceDescription = "Repertoire information on ".$resourceTitle;
			$resourceKeywords = $row['Title']." ".$resourceTitle; 
			
			$sql = "INSERT INTO Resources (ResourceTitle,ResourceDescription,ResourceKeywords,ResourceTypeID,StatusTypeID,CreatedBy,CreatedWhen)";
			$sql .= "VALUES ('".$resourceTitle."','".$resourceDescription."','".$resourceKeywords."','2','2','".$_POST['UserID']."','".$date->format('Y-m-d H:i:s')."')";
			echo "\n".$sql."<br>\n";
			$database->ExecuteSQL($sql);
//			echo $sql.mysql_error()."<br>";
//			echo mysql_insert_id()."<br>";
			$ResourceID = mysql_insert_id();
			
			$sql = "INSERT INTO RepertoireItems (`ResourceIDP`,`ComposerIDT`,`Title`,`Arranger`,`PrincipalKey`,`Organ`,`Voices`,`Publisher`,`Copyright`,`Words`)\n";
			$sql .= "VALUES ('".$ResourceID."','".$row['Composer']."','".$row['Title']."','".$row['Arranger']."','".$row['PrincipalKey']."','".$row['Organ']."','".$row['Voices']."','".$row['Publisher']."','".$row['Copyright']."','".addslashes($row['Words'])."')";
			echo "\n".$sql."<br>\n";
			$database->ExecuteSQL($sql);
			
			$sql = "INSERT INTO ChoralCategorys (ResourceID, ChoralCategoryNameID) VALUES ('".$ResourceID."','".$row['Category']."')";
			echo "\n".$sql."<br>\n";
			$database->ExecuteSQL($sql);

			$sql = "INSERT INTO Actions (ResourceID, ActionTypeID, ActionDate, ActionUserID, ActionComment) ";
			$sql .= "VALUES ('".$ResourceID."','2','".$date->format('Y-m-d H:i:s')."','".$_POST['UserID']."','Automated import')";
			echo $sql."<br>";
			$database->ExecuteSQL($sql);
			
		//	break;		
			
		}

		return $html;
		
		
	}
	protected function getContentsDiv()
	{
		$html = "<div id=\"content\">\n";
		$html .= "<h1>Test Page</h1>\n";
		
//		$html .= $this->ImportComposer();
		$html .= $this->ImportPieces();
		
		$html .= "</div>\n";
		return $html;
	}
	
}